package jobhunter.controller;

import java.io.UnsupportedEncodingException;

import jobhunter.model.Company;
import jobhunter.model.User;
import jobhunter.repository.CompanyRepository;
import jobhunter.repository.ProcessRepository;
import jobhunter.service.ActionService;
import jobhunter.service.ProcessService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class CompanyController 
{
	/*@Autowired
	private CompanyService companyService;*/

	@Autowired
	private CompanyRepository companyRepository;
	@Autowired
	private ActionService actionService;
	@Autowired
	private ProcessService processService;
	
	

	@Autowired
	private ProcessRepository processRepository;


	@RequestMapping("/companiesList")
	public ModelAndView listCompany()
	{
		ModelAndView mav = new ModelAndView("company/companiesList");
		User user = null;
		mav.addObject("companiesList", companyRepository.findByUser());
		return mav;
	}


	@RequestMapping("/company")
	public ModelAndView company(@RequestParam(value = "id") Long id,@RequestParam(value = "e", required = false) Long e)
	{
		Company company = companyRepository.findByIdWithContactAndProcess(id);	
		ModelAndView mav = new ModelAndView("company/company");
		mav.addObject("company",company);
		mav.addObject("lastProcess",processService.getLastProcessWithActionsByCompany(company));
		mav.addObject("typeError",e);
		return mav;
	}

	@RequestMapping("/delCompany")
	public String delCompany(@RequestParam(value = "id") Long id)
	{
		try
		{
			companyRepository.remove(id);
			return "redirect:companiesList";
		}
		catch(Exception e)
		{
			return "redirect:company?id="+id+"&e=1";
		}
	}

	@RequestMapping("/addCompany")
	public String addCompany()
	{
		return "company/addCompany";
	}

	@RequestMapping("/addCompanyValidate")
	public String addCompanyValidate(@RequestParam(value = "name") String name,
			@RequestParam(value = "website") String website,
			@RequestParam(value = "phone") String phone,
			@RequestParam(value = "email") String email,
			@RequestParam(value = "address") String address) throws UnsupportedEncodingException
	{
		name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
		website = new String(website.getBytes("ISO-8859-1"),"UTF-8"); 
		phone = new String(phone.getBytes("ISO-8859-1"),"UTF-8");
		email = new String(email.getBytes("ISO-8859-1"),"UTF-8");
		address = new String(address.getBytes("ISO-8859-1"),"UTF-8");
		
		Company company = new Company(name, website, phone, email, address);
		companyRepository.persist(company);
		return "redirect:companiesList";
	}



	@RequestMapping("/updCompany")
	public ModelAndView updCompany(@RequestParam(value = "id") Long id,
			@RequestParam(value = "contactId", required = false) Long contactId)
	{
		ModelAndView mav = new ModelAndView("company/updCompany");
		mav.addObject("company",companyRepository.findById(id));
		if(contactId!=null)
		{
			mav.addObject("contactId",contactId);
		}
		return mav;		
	}

	@RequestMapping("/updCompanyValidate")
	public String updCompanyValidate(@RequestParam(value = "id") Long id,
			@RequestParam(value = "name") String name,
			@RequestParam(value = "website") String website,
			@RequestParam(value = "phone") String phone,
			@RequestParam(value = "email") String email,
			@RequestParam(value = "address") String address,
			@RequestParam(value = "contactId", required = false) Long contactId) throws UnsupportedEncodingException
	{		
		name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
		website = new String(website.getBytes("ISO-8859-1"),"UTF-8"); 
		phone = new String(phone.getBytes("ISO-8859-1"),"UTF-8");
		email = new String(email.getBytes("ISO-8859-1"),"UTF-8");
		address = new String(address.getBytes("ISO-8859-1"),"UTF-8");
		
		Company company = companyRepository.findById(id);
		company.setName(name);
		company.setWebsite(website);
		company.setPhone(phone);
		company.setEmail(email);
		company.setAddress(address);
		companyRepository.merge(company);
		

		if(contactId!=null)
		{
			return "redirect:contact?id="+contactId;				
		}
		return "redirect:companiesList";
	}
}